define(function(require, exports, module){
	var ajax = require("resource/js/component/ajax.js");
	require("resource/js/easyui/easyloader.js");
	require("resource\js\component\rules.js");
	var alertMsg = require("resource/js/component/alerts.js");
	
	var sealId;
	
	var save = function(){
		var param = $("#sealform").serializeJson();
		if($('#sealform').form('validate')){
			ajax.init().success(function(code,msg,data){
				if(code == 0){
					alertMsg.show(msg);
					loadForm();
				}
			}).post(BASEPATH+'/oa/act/seal/save.htm',param);
		}

	};
	
	var update = function(){
		var param = $("#sealform").serializeJson();
		param.sealId = sealId;
		if($('#sealform').form('validate')){
			ajax.init().success(function(code,msg,data){
				if(code == 0){
					alertMsg.show(msg);
				}
			}).post(BASEPATH+'/oa/act/seal/update.htm',param);
		}
	};
	
	var del = function(){
		ajax.init().success(function(code,msg,data){
			if(code == 0){
				loadForm();
			}
		}).post(BASEPATH+'/oa/act/seal/delete.htm',{'sealId':sealId});
	};
	
	var loadForm = function(){
		ajax.init().success(function(code,msg,data){
			if(code == 0){
				if(data.length > 0){
					sealId = data[0].sealId;
					$('#sealform').form('load',data[0]);
					$('#sealImg').attr('src',BASEPATH+data[0].path).show();
					$('#update').show();
					$('#delete').show();
					$('#save').hide();
				}else{
					$('#save').show();
					$('#update').hide();
					$('#delete').hide();
					$('#sealImg').hide().removeAttr('src');
					$('#sealform').form('clear');
				}
			}else{
				alertMsg.show(msg);
			}
		}).post(BASEPATH+'/oa/act/seal/querySealByUser.htm');
	}
	
	var initUpload = function(){
		using("upload", function() {
			$("#path").upload({
				isMutil:false,
				isMultiple: false,
				param: {
					exts: 'png,jpg,bmp'
				},
				onSuccessAfter:function(files){
					var path = files[0].preview||files[0].fileUrl;
					$(this).val(path)
					$('#sealImg').attr('src',BASEPATH+path).show();
				},
				cropper:true
			});
			$('#upload').bind("click", function() {
				$("#path").upload("show");
			});
		});
	};

	module.exports={
		init:function(){
			using(['parser','dialog','textbox','linkbutton','upload'],function(){
				loadForm();
				initUpload();
				$('#save').bind('click',function(){
					save();
				});
				$('#update').bind('click',function(){
					update();
				});
				$('#delete').bind('click',function(){
					del();
				});
			});
		}
	}
});

